package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.model.KnowledgeBaseEntity;
import com.ytjj.qmyx.admin.model.request.KnowledgeBaseRequest;
import com.ytjj.qmyx.admin.model.request.KnowledgeImportResponse;
import com.ytjj.qmyx.admin.service.KnowledgeBaseService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author jiawei
 * @date 2024年08月22日 10:22
 * @describe
 */
@RestController
@RequestMapping("/knowledgeBase")
public class KnowledgeBaseController {
    @Resource
    KnowledgeBaseService knowledgeBaseService;

    /**
     * 导入知识库
     */
    @PostMapping("/importExcelData")
    public CommonResult<Map<String,Integer>> importExcelData(MultipartFile file){
        Map<String, Integer> stringIntegerMap = knowledgeBaseService.saveExcelData(file);
        return CommonResult.success(stringIntegerMap);
    }


    /**
     * 查询最近导入的记录
     */
    @PostMapping("/getRecentImportRecords")
    public CommonResult<KnowledgeImportResponse> getRecentImportRecords() {
        return CommonResult.success(knowledgeBaseService.getRecentImportRecords());
    }

    /**
     * 批量删除知识库条目
     */
    @PostMapping("/batchDelItem")
    public CommonResult batchDelItem(@RequestBody List<Integer> ids){
        Integer integer = knowledgeBaseService.batchDelItem(ids);
        return integer > 0 ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 新增或者编辑知识库条目
     */
    @PostMapping("/addOrEdit")
    public CommonResult addOrEdit(@RequestBody KnowledgeBaseEntity entity){
        Integer integer = knowledgeBaseService.addOrEdit(entity);
        return integer > 0 ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 知识库的查询接口
     */
    @PostMapping("/searchKnowledgeBase")
    public CommonResult<CommonPage<KnowledgeBaseEntity>> searchKnowledgeBase(@RequestBody KnowledgeBaseRequest request){
        CommonPage<KnowledgeBaseEntity> knowledgeBaseEntityCommonPage = knowledgeBaseService.searchKnowledgeBase(request);
        return CommonResult.success(knowledgeBaseEntityCommonPage);
    }






}

